﻿using ApiData.JsonDB;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ApiData.Logic
{
    public class Update
    {
        public static Common.ResultData Execute(Model.tables tab, Dictionary<string, string> pairs, int index)
        {
            var path = tab.jsondata;
            var phy = Directory.GetCurrentDirectory();
            var real = phy + path;

            JObject token = new JObject();
            foreach (var key in pairs.Keys)
            {
                JProperty jp = new JProperty(key, pairs[key]);
                token.Add(jp);
            }
            var db = new InitDB(real);
            bool isok = db.update(token, index);

            if (isok) return new Common.ResultData().Success();
            else return new Common.ResultData().Fail();
        }
    }
}
